package com.jnu.mydiary.ui.shouye1;

import android.content.Context;

import com.jnu.mydiary.ui.users.User;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class DataBank {
    private static Context context;
    private String DiaryFileName="myDiary";
    private String UserFileName="User";
    private List<myBean> mList;


    public DataBank(Context context) {
        this.context = context;
    }

    public List<myBean> loadData() {
        List<myBean> myList = new ArrayList<>();
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(context.openFileInput(DiaryFileName));
            myList = (ArrayList<myBean>) objectInputStream.readObject();
            mList=myList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return myList;
    }
    public void saveData(List<myBean> myList) {
        ObjectOutputStream objectOutputStream=null;
        try{
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(DiaryFileName, Context.MODE_PRIVATE));
            objectOutputStream.writeObject(myList);
            objectOutputStream.close();
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public User loadUser() {
      User mUser=null;
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(context.openFileInput(UserFileName));
            mUser = (User) objectInputStream.readObject();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mUser;
    }
    public void saveUser(User mUser) {
        ObjectOutputStream objectOutputStream=null;
        try{
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(UserFileName, Context.MODE_PRIVATE));
            objectOutputStream.writeObject(mUser);
            objectOutputStream.close();
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
